<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


include 'C:\wamp\www\Projects\eformalistics\Formalistics\index.php';

class ScheduledDelegate {

    public $db;

    public function __construct($db) {
        $this->db = $db;
    }

    private function checkRequestDaysPending($tblname) {
        $result = $this->db->query("SELECT ID, Node_ID, Workflow_ID, DateDiff(Now(),DateUpdated) as DaysPending FROM {$tblname}", "array");
        return $result;
    }

    private function getWorflowDefaultAction($worklow_id, $node_id) {
        $workflow_object = $this->db->query("SELECT json FROM tbworkflow_objects WHERE workflow_id = {$worklow_id} AND object_id={$this->db->escape($node_id)}", "row");
        $data = json_decode($workflow_object['json'], true);

        return $data;
    }

    public function getDelegateDays($company) {
        $delegates = functions::getKeywords("WHERE description = {$this->db->escape('Delegate Keyword')} "
                        . "AND company_id = $company->id");

        foreach ($delegates as $delegate) {
            return $delegate->quantity;
        }
    }

    public function getCompanyAdmin($company) {
        $admins = functions::getUsers("WHERE user_level_id='2' AND company_id = $company->id");

        foreach ($admins as $adminDoc) {
            return $adminDoc;
        }
    }

    public function processRequests() {
        // $date = $this->currentDateTime();
        $forms = functions::getWorkspace();

        foreach ($forms as $form) {
            $request_days_limit = $this->getDelegateDays($form->company);
            $company_admin = $this->getCompanyAdmin($form->company);

            $requests = $this->checkRequestDaysPending($form->form_table_name);
            foreach ($requests as $request) {
                print_r('please wait...');
                ?>

                <?php

                $requestDoc = new Request();
                $requestDoc->load($form->id, $request['ID']);
                $workflow_object = $this->getWorflowDefaultAction($request['Workflow_ID'], $request['Node_ID']);

                if ($request['DaysPending'] < $request_days_limit && $workflow_object['workflow-default-action'] != '') {
                    $fields['Node_ID'] = $workflow_object['workflow-default-action'];
                    $requestDoc->data = $fields;
                    $requestDoc->modify();

                    $logDoc = new Request_Log($this->db);
                    $logDoc->form = new Form($this->db, $form->id);
                    $logDoc->request_id = $request['ID'];
                    $logDoc->details = 'Processed through scheduled task. Request has not been processed for 3 days.';
                    $logDoc->created_by = $company_admin;
                    $logDoc->save();

                    $requestDoc->processWF();
                }
            }
        }
    }

}

$db = new Database;
$delegateDoc = new ScheduledDelegate($db);
$delegateDoc->processRequests();
